package com.imclient.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ClientServer {
	
	private Socket mSocket;
	
	private String host;
	
	private int port;
	
	private InputStream ips;
	
	private OutputStream ops;
	
	public ClientServer(String host,int port){
		this.host = host;
		this.port = port;
		
	}

	public InputStream getIps() {
		return ips;
	}


	public OutputStream getOps() {
		return ops;
	}


	public void setHost(String host) {
		this.host = host;
	}


	public int getPort() {
		return port;
	}


	public void setPort(int port) {
		this.port = port;
	}

	public void close(){
		if(mSocket!=null){
			if(!mSocket.isClosed()){
				try {
					mSocket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{}
			}
		}
		reset();
	}
	

	private  void reset(){
		
		if(ips!=null){
			try {
				ips.close();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{}
		}
		if(ops!=null){
			try {
				ops.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{}
		}
	} 
	
	public void start() throws IOException{
			if(host==null||port==0){
				throw new RuntimeException("host or ip is empty");
			}
			mSocket = new Socket(host, port);
			ips = mSocket.getInputStream();
			ops = mSocket.getOutputStream();
	}
	
	

}
